﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace WpfByToolKit.ViewModel
{
    public class B : ObservableRecipient
    {

    }

    public class A : ObservableValidator
    {

    }

    public partial class MainWindowViewModel : ObservableObject
    {
        [ObservableProperty]
        private int num1copy;


        //[ObservableProperty] //帮你生成一个公开属性，并触发引用地方的更新，改变本身了
        //[NotifyPropertyChangedFor(nameof(Num1copy))] //仅仅指明Num1copy引用的地方会变化，但是没有更新本身
        private int num1;

        public int Num1
        {
            get { return num1; }
            set
            {
                if (SetProperty<int>(ref num1, value))//or   OnPropertyChanged(nameof(Num1));
                {
                    Num1copy = value;
                }

            }
        }

        //[ObservableProperty]
        //[NotifyCanExecuteChangedFor(nameof(sayHelloCommand))]//必须同时添加ObservableProperty特性进行批注，num2变化触发命令执行“状态”
        private int num2;

        public int Num2
        {
            get { return num2; }
            set
            {
                //OnPropertyChanged(nameof(Num2));


                if (SetProperty(ref num2, value))
                {
                    sayHelloCommand.Execute(this);
                }

            }
        }

        private bool CanSayHello()
        {
            return true;
        }

        [RelayCommand(CanExecute = nameof(CanExecute))]
        private void sayHello()
        {
            Console.WriteLine($"Hello, NUM3 = {Num1 + Num2}");
            Num3 = Num1 + Num2;
        }
        //or
        //private void SayHello()
        //{
        //    Console.WriteLine("Hello");
        //}
        //private IRelayCommand? sayHelloCommand;
        //public IRelayCommand SayHelloCommand => sayHelloCommand ??= new RelayCommand(sayHello, CanSayHello);
        [ObservableProperty]
        private int num3;

        //public int Num3
        //{
        //	get { return num3; }
        //	set
        //	{
        //		OnPropertyChanged(nameof(Num3));
        //	}
        //}

        public IRelayCommand AddCommand { get; }
        public IRelayCommand BtnCommand { get; }



        private void Execute()
        {
            Num3 = Num1 + Num2;
        }
        private bool CanExecute()
        {
            return true;
        }

        public MainWindowViewModel()
        {
            AddCommand = new RelayCommand(Execute, CanExecute);
            BtnCommand = new RelayCommand(() =>
            {
                MessageBox.Show("触发btnAdd2事件了");
                WeakReferenceMessenger.Default.Send<string>("{{status:1000,data:[]}}");

                WeakReferenceMessenger.Default.Send<string, string>("{name:\"张三\",age:\"13\"}", "huiyuan");
            });




        }



        private string password;

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                OnPropertyChanged(nameof(Password));
            }
        }


    }
}
